package com.dsa.team5.kanban.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dsa.team5.kanban.model.Kanban;
import com.dsa.team5.kanban.model.Status;
import com.dsa.team5.kanban.model.Task;
import com.dsa.team5.kanban.service.KanbanService;
import com.dsa.team5.kanban.service.StatusService;
import com.dsa.team5.kanban.service.TaskService;
 
@Controller
public class KanbanController {
	
	@Autowired
	KanbanService kanbanService;
	
	@Autowired
	StatusService statusService;
	
	@Autowired
	TaskService taskService;
    
    @RequestMapping(value = "/kanban", method = RequestMethod.GET)
    public String showKanban(ModelMap model,
    		@RequestParam("code") String kanbanCode) {
        Kanban kanban = kanbanService.getKanbanByCode(kanbanCode);
        List<Status> status = statusService.getStatusByKanbanId(kanban.getKanbanId());
        model.addAttribute("status", status);
        model.addAttribute("kanbanCode", kanbanCode);
        return "kanban";
    }
    
    @RequestMapping(value = "/taskByStatus", method = RequestMethod.POST)
    public @ResponseBody List<Task> showTaskInStatus(@RequestParam("statusId") Long statusId) {
    	List<Task> tasks = taskService.getTaskByStatusId(statusId);
        return tasks;
    }  
   
    

    @RequestMapping(value = "/newTask", method = RequestMethod.POST)
    public @ResponseBody String addNewTask(
    		@RequestParam("code") String kanbanCode,
    		@RequestBody Task newTask) {
        Kanban kanban = kanbanService.getKanbanByCode(kanbanCode);
        List<Status> status = statusService.getStatusByKanbanId(kanban.getKanbanId());
    	newTask.setStatus(status.get(0));
    	taskService.save(newTask);
        return "success";
    }
    
    @RequestMapping(value = "/updateTask", method = RequestMethod.GET)
    public @ResponseBody String updateTask(
    		@RequestParam("taskId") Long taskId,
    		@RequestParam("statusId") Long statusId) {
    	Task task = taskService.getTaskByTaskId(taskId);
    	Status status = statusService.getStatus(statusId);
    	System.out.println(task.getTaskId()+"    "+status.getName());
    	task.setStatus(status);
    	taskService.update(task);
        return "success";
    }
}